﻿using Himp.Platform.Client.Blls;
using Himp.Platform.Client.Contracts;
using log4net;
using Newtonsoft.Json;
using ReaderB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Himp.Platform.Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        #region 写卡设备

        private SamplePreparationBll _samplePreparationBll;
        private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
        public static string UserName { get; set; } = "admin";

        #endregion

        #region 方法

        /// <summary>
        /// 
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            _samplePreparationBll = new SamplePreparationBll();
            wv2.CoreWebView2InitializationCompleted += (sender, args) =>
            {
                wv2.CoreWebView2.WebMessageReceived += async (sender1, args1) =>
                {
                    try
                    {
                        string webMessageModelString = args1.TryGetWebMessageAsString();
                        var webMessageModel = JsonConvert.DeserializeObject<WebMessageModel>(webMessageModelString);

                        if (webMessageModel.Type == 1) //写卡
                        {
                            var input = JsonConvert.DeserializeObject<SamplePreparationRecCreateDto>(webMessageModel.Content);
                            var result = await _samplePreparationBll.WriteSamplePreparationRec(input);
                            await wv2.CoreWebView2.ExecuteScriptAsync($"window.receiveMessageFromWPF('{result}')");
                        }
                    }
                    catch (Exception e)
                    {
                        log.Error(e);
                        await wv2.CoreWebView2.ExecuteScriptAsync($"window.receiveMessageFromWPF('{e.Message}')");
                    }
                };
            };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void Write_Click(object sender, RoutedEventArgs e)
        {
            SamplePreparationRecCreateDto samplePreparationRecCreateDto = new SamplePreparationRecCreateDto();
            samplePreparationRecCreateDto.SampleID = DateTime.Now.ToString("yyyyMMddHHmmss");
            samplePreparationRecCreateDto.SampleType = 4;
            samplePreparationRecCreateDto.Size = 3;
            samplePreparationRecCreateDto.SampleWeight = 1000;
            samplePreparationRecCreateDto.UserName = "admin";

            await _samplePreparationBll.WriteSamplePreparationRec(samplePreparationRecCreateDto);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Read_Click(object sender, RoutedEventArgs e)
        {
            _samplePreparationBll.ReadSamplePreparationRec();
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        public class WebMessageModel
        {
            public int Type { get; set; }

            public string Content { get; set; }
        }
    }
}
